package com.gy.def.controller;

import com.gy.def.dataobject.BookTable;
import com.gy.def.dataobject.CourseTable;
import com.gy.def.dataobject.UserTable;
import com.gy.def.enums.ResultEnum;
import com.gy.def.exception.GySchoolException;
import com.gy.def.service.BookService;
import com.gy.def.service.UserService;
import com.gy.def.utils.ResultVoUtil;
import com.gy.def.viewobject.PageMap;
import com.gy.def.viewobject.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@CrossOrigin // 允许跨域
@Api(value = "BookController", description = "图书馆模块")
@RestController
@RequestMapping("/book")
public class BookController {

  @Autowired
  private BookService bookService;

  @Autowired
  private UserService userService;

  @ApiOperation(value = "添加书籍", notes = "添加书籍")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "bookName", value = "书名", required = true, dataType = "string")
  })
  @PostMapping("/addCourse")
  public ResultVo addBook(@RequestParam("bookName") String bookName) {
    BookTable bt = bookService.findByBookName(bookName);
    if (bt != null) {
      throw new GySchoolException(ResultEnum.ErrorBookRepetition);
    }

    bt = new BookTable();
    bt.setBookName(bookName);

    BookTable result = bookService.save(bt);
    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }

    return ResultVoUtil.success();
  }

  @ApiOperation(value = "获取书籍列表", notes = "获取书籍列表")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "page", value = "第几页", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "一页有多少个数据", required = true, dataType = "string"),
  })
  @PostMapping("/getBookList")
  public ResultVo<PageMap<BookTable>> getBookList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                                  @RequestParam(value = "pageSize", defaultValue = "0") Integer pageSize) {
    Page<BookTable> mapList;
    PageRequest pageRequest = new PageRequest(page, pageSize);
    mapList = bookService.findByIsDelete(0, pageRequest);

    PageMap<BookTable> pm = new PageMap<>();
    pm.setNumber(mapList.getNumber());
    pm.setResultList(mapList.getContent());
    pm.setSize(mapList.getSize());
    pm.setTotalElements(mapList.getTotalElements());
    pm.setTotalPages(mapList.getTotalPages());

    return ResultVoUtil.success(pm);
  }

  @ApiOperation(value = "删除书籍", notes = "删除书籍")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "bookId", value = "书籍id", required = true, dataType = "string")
  })
  @PostMapping("/deleteBook")
  private ResultVo deleteBook(@RequestParam("bookId") String bookId) {
    BookTable bt = bookService.findByBookId(bookId);
    if (bt == null) {
      throw new GySchoolException(ResultEnum.ErrorGetBookNULL);
    }

    if (bt.getBookStatus() != 0) {
      throw new GySchoolException(ResultEnum.ErrorBookStatus);
    }

    bt.setIsDelete(1);
    BookTable result = bookService.save(bt);
    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }
    return ResultVoUtil.success();
  }

  @ApiOperation(value = "借书", notes = "借书")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "bookId", value = "书籍id", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "bookBorTime", value = "借书时间", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "bookRebackTime", value = "还书时间", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "username", value = "账户名", required = true, dataType = "string")
  })
  @PostMapping("/borBook")
  private ResultVo borBook(@RequestParam("bookId") String bookId,
                           @RequestParam("bookId") String bookBorTime,
                           @RequestParam("bookId") String bookRebackTime,
                           @RequestParam("username") String username) {
    BookTable bt = bookService.findByBookId(bookId);
    if (bt == null) {
      throw new GySchoolException(ResultEnum.ErrorGetBookNULL);
    }

    UserTable ut = userService.findByUserName(username);
    if (ut == null) {
      throw new GySchoolException(ResultEnum.GetUserAccountNULL);
    }

    bt.setBookStatus(1);
    bt.setBookRebackTime(bookRebackTime);
    bt.setBookBorTime(bookBorTime);
    bt.setBookUserId(ut.getUserId());

    BookTable result = bookService.save(bt);
    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }
    return ResultVoUtil.success();
  }

  @ApiOperation(value = "获取我借的书", notes = "获取我借的书")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "username", value = "账号", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "page", value = "第几页", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "一页有多少个数据", required = true, dataType = "string")
  })
  @PostMapping("/getMyBook")
  private ResultVo<PageMap<BookTable>> getMyBook(@RequestParam("username") String username,
                                                 @RequestParam(value = "page", defaultValue = "0") Integer page,
                                                 @RequestParam(value = "pageSize", defaultValue = "0") Integer pageSize) {
    UserTable ut = userService.findByUserName(username);
    if (ut == null) {
      throw new GySchoolException(ResultEnum.GetUserAccountNULL);
    }

    Page<BookTable> mapList;
    PageRequest pageRequest = new PageRequest(page, pageSize);
    mapList = bookService.findByBookUserId(ut.getUserId(), pageRequest);

    PageMap<BookTable> pm = new PageMap<>();
    pm.setNumber(mapList.getNumber());
    pm.setResultList(mapList.getContent());
    pm.setSize(mapList.getSize());
    pm.setTotalElements(mapList.getTotalElements());
    pm.setTotalPages(mapList.getTotalPages());

    return ResultVoUtil.success(pm);
  }
}
